<template>
  <div class="app-container">
    <div class="Search">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="query">

          <el-form-item v-has="'PLATFROM_TENANT_QUERY'">
            <el-cascader ref="tenantTree" placeholder="所属租户" :props="tenantProps" :options="tenantOptions"
              @change="selectTenant" clearable collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-cascader ref="organTree" placeholder="所属组织" :props="props" :options="organOptions" @change="selectOrgan"
              collapse-tags clearable filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.typeName" placeholder="分类名称" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.parentId" placeholder="上级id" />
          </el-form-item>
          <el-form-item>
            <el-button v-has="'GOODS_TYPE_QUERY'" type="primary" icon="el-icon-search" size="mini"
              @click="fetchData">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button v-has="'GOODS_TYPE_ADD'" type="success" icon="el-icon-plus" size="mini"
              @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>
    <div class="block">
      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row
        row-key="id">
        <el-table-column align="center" label="序号" width="95">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="分类名称" width="110">
          <template slot-scope="scope">
            {{ scope.row.typeName }}
          </template>
        </el-table-column>
        <el-table-column label="分类路径" width="110">
          <template slot-scope="scope">
            {{ scope.row.typePath }}
          </template>
        </el-table-column>
        <el-table-column label="所属机构" width="110" prop="organId" :formatter="translateOrgan" />
        <el-table-column label="上级id" width="110">
          <template slot-scope="scope">
            {{ scope.row.parentId }}
          </template>
        </el-table-column>
        <el-table-column label="分类排序" width="110">
          <template slot-scope="scope">
            {{ scope.row.sort }}
          </template>
        </el-table-column>
        <el-table-column label="备注" width="110">
          <template slot-scope="scope">
            {{ scope.row.remark }}
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" prop="created_at" label="操作">
          <template slot-scope="scope">
            <el-link v-has="'SUB_GOODS_TYPE_MODIFY'" icon="el-icon-edit" type="warning" style="cursor: pointer;"
              @click="handleEdit(scope.$index, scope.row)">
              修改
            </el-link>
            <el-link v-has="'SUB_GOODS_TYPE_REMOVE'" class="el-icon-delete" type="danger" style="cursor: pointer;"
              @click="handleDelete(scope.$index, scope.row)">
              删除
            </el-link>
            <el-link v-has="'SUB_GOODS_TYPE_ADD'" class="el-icon-share" type="success" style="cursor: pointer;"
              @click="handleSubAdd(scope.$index, scope.row)">
              添加下级类型
            </el-link>
            <el-link v-has="'GOODS_TYPE_PROPS_LIST'" class="el-icon-news" type="success" style="cursor: pointer;"
              @click="handleShowGoodsTypeProps(scope.$index, scope.row)">
              添加分类属性
            </el-link>
            <el-link v-has="'GOODD_TYPE_ASSIGN_2_ORGANS'" type="success" class="el-icon-s-custom"
              style="cursor: pointer;" @click="handleAssignToOrgan(scope.$index, scope.row)">
              分配给
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item v-if="addForVisible" label="分类id">
            <el-input v-model="form.id" placeholder="分类id" disabled auto-complete="off" />
          </el-form-item>
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'" label="所属租户" placeholder="所属租户" required>
            <el-cascader v-model="form.tenantId" ref="tenantTreeForm" placeholder="所属租户" :props="tenantProps"
              :options="tenantOptions" @change="chooseTenantForm(form)" style="width:100%" clearable collapse-tags
              filterable></el-cascader>
          </el-form-item>
          <el-form-item label="所属组织">
            <el-cascader v-model="form.organId" ref="organTreeForm" placeholder="所属组织" :props="props"
              :options="organFormOptions" @change="chooseOrganForm(form)" style="width:100%" collapse-tags
              filterable></el-cascader>
          </el-form-item>
          <el-form-item label="分类名称">
            <el-input v-model="form.typeName" placeholder="分类名称" auto-complete="off" />
          </el-form-item>
          <el-form-item label="分类路径" v-if="false">
            <el-input v-model="form.typePath" placeholder="分类路径" auto-complete="off" />
          </el-form-item>
          <el-form-item v-if="addSubGoodsType" label="上级分类编号">
            <el-input v-model="form.parentId" placeholder="上级id" disabled auto-complete="off" />
          </el-form-item>
          <el-form-item label="分类排序">
            <el-input v-model="form.sort" placeholder="分类排序" auto-complete="off" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" placeholder="备注" auto-complete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button v-has="'GOODS_TYPE_SAVE'" type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" width="68%" :visible.sync="dialogShowGoodsTypePropsVisible">
        <div class="Search">
          <!--工具条-->
          <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">
              <el-form-item>
                <el-input v-model="query.goodsTypeProps.typePropsName" placeholder="分类属性名称" auto-complete="off" />
              </el-form-item>
              <el-form-item>
                <el-button v-has="'GOODS_TYPE_PROPS_LIST'" type="primary"
                  @click="handleQueryGoodsTypeProps">查询</el-button>
              </el-form-item>
              <el-form-item>
                <el-button v-has="'GOODS_TYPE_PROPS_ADD'" type="success"
                  @click="handleAddGoodsTypeProps">添加分类属性</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </div>
        <el-table v-loading="listLoading" :data="goodsTypePropsList" element-loading-text="Loading" border fit
          @expand-change="expandChange" highlight-current-row>
          <el-table-column type="expand" align="center" label="" width="58">
            <template slot-scope="scope">
              <el-table v-loading="listLoading" :data="goodsTypePropsValueList" element-loading-text="Loading" border
                fit highlight-current-row>
                <el-table-column align="center" label="序号" width="58">
                  <template slot-scope="scope">
                    {{ scope.$index }}
                  </template>
                </el-table-column>
                <el-table-column label="分类属性值" width="110">
                  <template slot-scope="scope">
                    {{ scope.row.goodsPropsValue }}
                  </template>
                </el-table-column>
                <el-table-column label="备注">
                  <template slot-scope="scope">
                    {{ scope.row.remark }}
                  </template>
                </el-table-column>
                <el-table-column align="center" fixed="right" prop="created_at" label="操作">
                  <template slot-scope="scope">
                    <el-link v-has="'SUB_GOODS_TYPE_MODIFY'" icon="el-icon-edit" type="warning" style="cursor: pointer;"
                      @click="handleGoodsTypePropsValueEdit(scope.$index, scope.row)">
                      修改
                    </el-link>
                    <el-link v-has="'SUB_GOODS_TYPE_REMOVE'" class="el-icon-delete" type="danger"
                      style="cursor: pointer;" @click="handleDelete(scope.$index, scope.row)">
                      删除
                    </el-link>
                  </template>
                </el-table-column>
              </el-table>
              <div class="block">
                <el-pagination :current-page="query.goodsTypePropsValue.currentPage" :page-sizes="[10, 20, 30, 40]"
                  :page-size="query.goodsTypePropsValue.pageSize" layout="total, sizes, prev, pager, next, jumper"
                  :total="query.goodsTypePropsValue.total" @size-change="handleQueryPropsValSizeChange"
                  @current-change="handleQueryPropsValCurrentChange" />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="分类属性名称" width="110">
            <template slot-scope="scope">
              {{ scope.row.propName }}
            </template>
          </el-table-column>
          <el-table-column label="分类属性代码" width="110">
            <template slot-scope="scope">
              {{ scope.row.propCode }}
            </template>
          </el-table-column>
          <el-table-column label="备注" width="110">
            <template slot-scope="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="created_at" label="操作">
            <template slot-scope="scope">
              <el-link icon="el-icon-edit" type="warning" @click="handleGoodsTypePropsEdit(scope.$index, scope.row)">
                修改
              </el-link>
              <el-link class="el-icon-delete" type="danger" @click="handleDelete(scope.$index, scope.row)">
                删除
              </el-link>
              <el-link v-has="'GOODS_TYPE_PROPS_VALUE_ADD'" class="el-icon-share" type="success"
                @click="handleAddGoodsTypePropsValue(scope.$index, scope.row)">
                添加属性值
              </el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination :current-page="query.goodsTypeProps.currentPage" :page-sizes="[10, 20, 30, 40]"
            :page-size="query.goodsTypeProps.pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="query.goodsTypeProps.total" @size-change="handleQueryPropsSizeChange"
            @current-change="handleQueryPropsCurrentChange" />
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogShowGoodsTypePropsVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogAssignOrganFormVisible">
        <el-tree ref="assigneeTree" :data="organTree" :default-expand-all="true" :props="organTreeProps"
          :check-strictly="false" show-checkbox node-key="id" highlight-current />
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAssignOrganFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="getCheckedNodes">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogGoodsTypePropsValueFormVisible">
        <el-form :model="form">
          <el-form-item v-if="addGoodsTypePropsValueForVisible" label="主键">
            <el-input v-model="goodsTypePropsValue.id" placeholder="主键" auto-complete="off" />
          </el-form-item>
          <el-form-item label="关联商品属性">
            <el-input v-model="goodsTypePropsValue.goodsPropsId" placeholder="关联商品属性" auto-complete="off" disabled/>
          </el-form-item>
          <el-form-item label="商品分类">
            <el-cascader  v-model="goodsTypePropsValue.goodsTypeId"  ref="goodsTypeTreeForm" placeholder="商品分类" :props="goodsTypeTreeProps"
              :options="goodsTypeTreeOption" @change="getGoodsType(goodsTypePropsValue)" filterable></el-cascader>
          </el-form-item>
          <el-form-item label="商品属性值">
            <el-input v-model="goodsTypePropsValue.goodsPropsValue" placeholder="商品属性值" auto-complete="off" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="goodsTypePropsValue.remark" placeholder="备注" auto-complete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogGoodsTypePropsValueFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditGoodsTypePropsValueSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogGoodsTypePropsFormVisible">
        <el-form :model="form">

          <el-form-item v-if="addForGoodsTypePropsVisible" label="编号">
            <el-input v-model="goodsTypeProps.id" placeholder="主键" auto-complete="off" />
          </el-form-item>
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'" label="所属租户" placeholder="所属租户" required>
            <el-cascader v-model="goodsTypeProps.tenantId" ref="tenantTreeForm" placeholder="所属租户" :props="tenantProps"
              :options="tenantOptions" @change="chooseTenantForm(goodsTypeProps)" style="width:100%" clearable
              collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item label="所属组织">
            <el-cascader v-model="goodsTypeProps.organId" ref="organTreeForm" placeholder="所属组织" :props="props"
              :options="organFormOptions" @change="chooseOrganForm(goodsTypeProps)" style="width:100%" collapse-tags
              filterable></el-cascader>
          </el-form-item>
          <el-form-item label="关联商品分类">
            <el-cascader  v-model="goodsTypeProps.goodsTypeId" style="width: 100%;"  ref="goodsTypeTreeForm" placeholder="商品分类" :props="goodsTypeTreeProps"
              :options="goodsTypeTreeOption" @change="getGoodsType(goodsTypeProps)" filterable></el-cascader>
          </el-form-item>
    
          <el-form-item label="分类属性名称">
            <el-input v-model="goodsTypeProps.propName" placeholder="分类属性名称" auto-complete="off" />
          </el-form-item>          
          <el-form-item label="分类属性代码">
            <el-input v-model="goodsTypeProps.propCode" placeholder="分类属性代码" auto-complete="off" />
          </el-form-item>
          <el-form-item label="是否必选">
            <el-radio-group v-model="goodsTypeProps.isMust">
              <el-radio :label="true">必选</el-radio>
              <el-radio :label="false">可选</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否多选">
            <el-radio-group v-model="goodsTypeProps.isMutilSelect">
              <el-radio :label="true">多选</el-radio>
              <el-radio :label="false">单选</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否导航">
            <el-radio-group v-model="goodsTypeProps.isNav">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" placeholder="备注" auto-complete="off" />
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogGoodsTypePropsFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditGoodsTypePropsSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>

</template>
<script>
import { getList, doDelete, doEdit, assignee2Organ, getGoodsTypeTree, queryAlreadyAssignedOrgans } from '@/api/GoodsType/'
import { getList as getGoodsTypePropsList, doDelete as deleteGoodsTypeProps, doEdit as saveGoodsTypesProps } from '@/api/GoodsTypeProps/'
import { getList as getGoodsTypePropsValueList, doDelete as deleteGoodsTypePropsValue, doEdit as saveGoodsTypesPropsValue } from '@/api/GoodsTypePropsValue/'
import { querySubordinateOrgan, queryAssigneeOrgan } from '@/api/Organ'
import { getOrganId, getTenantId, getOrganPath, getCurrentLoginUserId, handleBuildTreeDataStruct, getSelectedNodes, getSelectedNodesObject, getCurrentTimeYYYY_MM_dd_HH_mm_ss } from '@/api/utils'
import { getTenantList, getOrganList, getDictionary, translateOrganId2OrganName } from '@/utils/auth'
import localStorageUtil from '@/utils/localStorageUtil'

export default {
  data() {
    return {
      list: [],
      listLoading: true,
      total: 5,
      currentPage: 1,
      pageSize: 10,
      dialogFormVisible: false,
      addForVisible: false,
      dialogShowGoodsTypePropsVisible: false,
      dialogFormGoodsTypePropsValueVisible: false,
      addGoodsTypePropsValueForVisible: false,
      addGoodsTypePropsValue: false,
      dialogAssignOrganFormVisible: false,
      dialogGoodsTypePropsFormVisible: false,
      addForGoodsTypePropsVisible: false,
      addForGoodsTypePropsValueVisible: false,
      dialogGoodsTypePropsValueFormVisible: false,
      addSubGoodsType: true,
      organTree: [],
      form: {
        id: '',
        organId: '',
        organPath: '',
        typeName: '',
        typePath: '',
        parentId: '',
        sort: '',
        isDeleted: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        updateDate: '',
        remark: ''
      },
      goodsTypeProps: {
        id: '',
        goodsTypeId: '',
        goodsTypePath: '',
        propName: '',
        propCode: '',
        organId: '',
        organPath: '',
        isDeleted: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        updateDate: '',
        remark: '',
        isMust: null,
        isMutilSelect: null,
        isNav: null
      },
      goodsTypePropsValue: {
        id: '',
        goodsPropsId: '',
        goodsPropsValue: '',
        goodsTypeId: '',
        goodsTypePath: '',
        organId: '',
        organPath: '',
        isDeleted: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        updateDate: '',
        remark: ''
      },
      goodsTypeAssignees: {
        id: '',
        assignees: [],
        tenantId: '',
        organId: ''
      },
      goodsTypePropsList: [], //分类属性
      goodsTypePropsValueList: [], //分类属性值
      title: '',
      // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      titleMap: {
        add: '新增',
        edit: '编辑',
        subAdd: '新增子级',
        showGoodsTypeProps: '显示分类属性',
        addGoodsTypeProps: '添加商品分类属性',
        addGoodsTypePropsValue: '添加商品分类属性值',
        assignOrgan: '请选择需要指派的组织'

      },
      isClearable: true, // 可清空（可选）
      isAccordion: true, // 可收起（可选）
      valueId: null, // 初始ID（可选）
      props: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '组织',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      organTreeProps: {
        children: 'children',
        label: 'organName',
        url: 'url'
      },
      tenantProps: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '租户',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      goodsTypeTreeProps: { // 配置项（必选）
        value: 'id',
        label: 'typeName',
        children: 'children',
        checkStrictly: true,
        title: '组织'
        // disabled:true
      },
      goodsTypeTreeOption: [],
      organFormOptions: [],
      tenantOptions: [],
      organOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)    
      dialogStatus: '',
      optionData: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      query: {
        params: {
          id: '',
          organId: '',
          organPath: '',
          organIds: [],
          typeName: '',
          typePath: '',
          parentId: '',
          sort: '',
          isDeleted: '',
          createBy: '',
          createDate: '',
          updateBy: '',
          updateDate: '',
          remark: ''
        },
        goodsTypeProps: {
          organPath: '',
          propName: '',
          goodsTypeId: '',
          total: 5,
          currentPage: 1,
          pageSize: 10
        },
        goodsTypePropsValue: {
          organPath: '',
          organId: '',
          tenantId: '',
          goodsPropsId: '',
          total: 5,
          currentPage: 1,
          pageSize: 10
        },
        organParams: {
          organPath: ''
        },
        goodsTypesOrganQuery: {
          goodsTypeId: '',
          organId: '',
          tenantId: ''
        },
        timeValue: '',
        options: [{
          value: '1',
          label: '黄金糕'
        }, {
          value: '2',
          label: '双皮奶'
        }],
        value: ''
      }
    }
  },
  created() {
    this.initOrganTree(() => {
      this.setOrganQueryType()
    }, getOrganPath())
    this.initOrganFormTree()
    this.initGoodsTypeTree()
    this.handleFiltTenant()
    this.fetchData()
  },
  methods: {
    translateOrgan(row) {
      return translateOrganId2OrganName(row);
    },
    fetchData() {
      this.listLoading = true
      this.createOrganQuery()
      this.currentPage = this.currentPage || 1
      this.pageSize = this.pageSize || 10
      getList(this.query.params, this.currentPage, this.pageSize).then(response => {
        const data = response.data
        if (!data.entities || typeof data.successful != 'undefined') {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
        this.total = data.totalCount
        this.pageSize = data.pageSize
        this.currentPage = data.currentPage
        this.list = handleBuildTreeDataStruct(data.entities, 'id', 'parentId')
        this.listLoading = false
      })
    },
    createOrganQuery() {
      // 选中了租户
      let tenantSelected = this.$refs['tenantTree'] && this.$refs['tenantTree'].getCheckedNodes() || []
      let organSelected = this.$refs['organTree'] && this.$refs['organTree'].getCheckedNodes() || []
      let that = this
      this.query.params.organId = null
      if (tenantSelected.length && tenantSelected[0]) {
        this.query.params.tenantId = tenantSelected[0].data.id
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0] && organSelected[0].data.organPath
          }
        }
      } else {
        this.query.params.tenantId = getTenantId()
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0].data.organPath
          }
        } 
      }
      this.query.params.organId =  this.query.params.organId ||  getOrganId()
      this.query.params.organPath = this.query.params.organPath || getOrganPath()
    },
    handleSizeChange(val) {
      debugger
      this.currentPage = 1
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    // 新增
    handleAdd() {
      // 显示弹框
      this.dialogFormVisible = true
      // 新增弹框标题
      this.dialogStatus = 'add'

      this.form.id = ''
      this.form.organId = getOrganId()
      this.form.tenantId = getTenantId()
      this.form.organPath = getOrganPath()
      this.form.typeName = ''
      this.form.typePath = ''
      this.form.parentId = ''
      this.form.sort = ''
      this.form.isDeleted = ''
      this.form.createBy = getCurrentLoginUserId()
      this.form.createDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      this.form.updateBy = ''
      this.form.updateDate = ''
      this.form.remark = ''
      this.addForVisible = false
      this.addSubGoodsType = false
    },
    // 编辑
    handleEdit(index, row) {

      this.form.id = row.id
      this.form.organId = row.organId || getOrganId()
      this.form.tenantId = row.tenantId || getTenantId()
      this.form.organPath = row.organPath || getOrganPath()

      this.form.typeName = row.typeName
      this.form.typePath = row.typePath
      this.form.parentId = row.parentId
      this.form.sort = row.sort
      this.form.createBy = row.createBy
      this.form.createDate = row.createDate
      this.form.updateBy = getCurrentLoginUserId()
      this.form.updateDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      this.form.remark = row.remark
      this.addForVisible = true
      // 显示弹框
      this.dialogFormVisible = true
      // 编辑弹框标题
      this.dialogStatus = 'edit'
    },
    handleSubAdd(index, row) {

      // 显示弹框
      this.dialogFormVisible = true
      // 新增弹框标题
      this.dialogStatus = 'subAdd'
      this.addSubGoodsType = true
      this.form.id = ''
      this.form.organId = row.id || getOrganId()
      this.form.tenantId = row.tenantId || getTenantId()
      this.form.organPath = row.organPath || getOrganPath()
      this.form.typeName = ''
      this.form.typePath = '/' + row.id
      this.form.parentId = row.id
      this.form.sort = ''
      this.form.isDeleted = ''
      this.form.createBy = getCurrentLoginUserId()
      this.form.createDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      this.form.updateBy = getCurrentLoginUserId()
      this.form.updateDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      this.form.updateBy = ''
      this.form.updateDate = ''
      this.form.remark = ''
      this.addForVisible = false

    },
    handleEditGoodsTypePropsSave() {

      this.listLoading = true
      this.$confirm('是否确定保存所填商品分类属性?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        saveGoodsTypesProps(this.goodsTypeProps).then(response => {
          const gateway = response.data
          if (gateway.successful) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
          } else {
            this.$message({
              type: 'info',
              message: gateway.description || getSystemErrorCode(gateway.status)
            })
          }
          this.listLoading = true
          this.handleQueryGoodsTypeProps()
        })
      })
    },
    handleShowGoodsTypeProps(index, row) {

      // 显示弹框
      this.dialogShowGoodsTypePropsVisible = true
      // 新增弹框标题
      this.dialogStatus = 'showGoodsTypeProps'
      this.query.goodsTypeProps.goodsTypeId = row.id
      this.query.goodsTypeProps.organId = row.organId
      this.query.goodsTypeProps.organPath = row.organPath
      this.query.goodsTypeProps.tenantId = row.tenantId

      this.handleQueryGoodsTypeProps()

    },
    handleAddGoodsTypeProps() {
      // 显示弹框
      this.dialogGoodsTypePropsFormVisible = true
      // 新增弹框标题
      this.dialogStatus = 'addGoodsTypeProps'
      this.goodsTypeProps.goodsTypeId = this.query.goodsTypeProps.goodsTypeId
      this.goodsTypeProps.id = ''
      this.goodsTypeProps.tenantId = getTenantId()
      this.goodsTypeProps.organId = getOrganId()
      this.goodsTypeProps.organPath = getOrganPath()
    },
    handleEditGoodsTypeProps(index, row) {

      // 显示弹框
      this.dialogFormGoodsTypePropsVisible = true
      // 新增弹框标题
      this.dialogStatus = 'addGoodsTypePropsValue'
      this.goodsTypeProps.goodsTypeId = row.id
      this.goodsTypeProps.goodsTypePath = row.path
    },
    handleEditGoodsTypePropsValue(index, row) {

      // 显示弹框
      this.dialogFormGoodsTypePropsVisible = true
      // 新增弹框标题
      this.dialogStatus = 'addGoodsTypePropsValue'
      this.goodsTypePropsValue.goodsTypeId = row.id
      this.goodsTypePropsValue.organId = row.organId
      debugger
      this.goodsTypePropsValue.organPath = row.organPath
      this.goodsTypePropsValue.tenantId = row.tenantId

    },
    handleQueryGoodsTypeProps() {
      this.listLoading = true
      this.query.goodsTypeProps.currentPage = this.query.goodsTypeProps.currentPage || 1,
        this.query.goodsTypeProps.pageSize = this.query.goodsTypeProps.pageSize || 10
      getGoodsTypePropsList(this.query.goodsTypeProps,
        this.query.goodsTypeProps.currentPage,
        this.query.goodsTypeProps.pageSize
      ).then(response => {
        const pager = response.data
        console.log(pager)
        this.goodsTypePropsList = pager.entities
        this.query.goodsTypeProps.pageSize = pager.pageSize
        this.query.goodsTypeProps.currentPage = pager.currentPage
        this.query.goodsTypeProps.total = pager.totalCount

        this.listLoading = false
      })
    },

    handleQueryPropsSizeChange(val) {
      this.query.goodsTypeProps.pageSize = val
      this.query.goodsTypeProps.currentPage = 1
      this.handleQueryGoodsTypeProps()
    },
    handleQueryPropsCurrentChange(val) {

      this.query.goodsTypeProps.currentPage = val
      this.handleQueryGoodsTypeProps()
    },
    handleAddGoodsTypePropsValue(index, row) {
      // 显示弹框
      this.dialogGoodsTypePropsValueFormVisible = true
      // 新增弹框标题
      this.dialogStatus = 'addGoodsTypePropsValue'
      this.goodsTypePropsValue.id = ''
      this.goodsTypePropsValue.goodsPropsId = row.id
      this.goodsTypePropsValue.organId = row.organId || getOrganId()
      this.goodsTypePropsValue.organPath = row.organPath || getOrganPath()
      this.goodsTypePropsValue.tenantId = row.tenantId || getTenantId()
      this.goodsTypePropsValue.createBy = getCurrentLoginUserId()
      this.goodsTypePropsValue.goodsTypeId = row.goodsTypeId
      this.goodsTypePropsValue.goodsPropsValueName = row.propName

    },
    handleEditGoodsTypePropsValueSave() {

      this.$confirm('是否确定添加所填写商品分类属性值?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        saveGoodsTypesPropsValue(this.goodsTypePropsValue).then(response => {

          const gateway = response.data
          if (gateway.successful) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.handleQueryGoodsTypePropsValueList()

          } else {
            this.$message({
              type: 'info',
              message: gateway.description || getSystemErrorCode(gateway.status)
            })
          }
        })
        this.listLoading = true

      })
    },
    handleQueryGoodsTypePropsValues() {
      getGoodsTypePropsValueList(this.goodsTypePropsValue).then(response => {

        const gateway = response.data
        let data = gateway.data
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        } else {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
      })
    },
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {

        let removeVO = {id:row.id, organId: row.organId, tenantId: row.tenantId}
        doDelete(removeVO)
          .then(response => {
            const data = response.data
            console.log(data)
            if (data.status == 1) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.fetchData();
            } else if (data.status == 403) {
              this.$message({
                type: 'warning',
                message: data.description
              })
            } else {
              this.$message({
                type: 'info',
                message: '操作失败!'
              })
            }
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEditSave() {

      this.listLoading = true
      this.form.createBy = getCurrentLoginUserId()
      this.form.updateBy = getCurrentLoginUserId()
      this.form.assignee = { id: getOrganId(), organPath: getOrganPath() }
      doEdit(this.form).then(response => {

        const data = response.data
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        } else {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }

        this.listLoading = false
        this.dialogFormVisible = false
      })
    },
    handleReset() {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    initOrganTree(fn, filter, that) {
      this.listLoading = true
      let data = getOrganList()
      if (filter) {
        data = data.filter(function (item, index) {
          return item.organPath.indexOf(filter) >= 0
        })
      }
      if (that && that.organFormOptions && that.organFormOptions.length >= 0) {
        that.organFormOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      } else {
        this.organOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      }
      this.listLoading = false
      if (fn) {
        fn()
      }
    },
    initOrganFormTree(fn, filter) {
      this.initOrganTree(fn, filter, this)
    },
    initGoodsTypeTree() {
      this.listLoading = true
      getGoodsTypeTree({ organId: getOrganId(), tenantId: getTenantId() }).then(response => {
        console.log(response)
        const data = response.data.data
        this.goodsTypeTreeOption = handleBuildTreeDataStruct(data, 'id', 'parentId')
        this.listLoading = false
      })
    },
    setOrganQueryType() {
      this.organQueryModelChange(localStorageUtil.fetch("SYSTEM_SETTING_ORGAN_QUERY_MODEL"))
    },
    selectOrgan(nodes) {
      let nodesInfo = this.$refs['organTree'].getCheckedNodes()
      let that = this
      this.query.params.organPath = ''
      this.query.params.organIds = []
      if (this.props.multiple) {
        this.query.params.organIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function (element) {
            that.query.params.organIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.organPath = nodesInfo[0].data.organPath
      }
    },
    selectTenant(nodes) {
      let nodesInfo = this.$refs['tenantTree'].getCheckedNodes()
      let that = this
      this.query.params.tenantId = ''
      this.query.params.tenantIds = []
      this.query.params.organId = null
      this.query.params.organIds = []
      this.query.params.organPath = null

      if (this.tenantProps.multiple) {
        this.query.params.tenantIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function (element) {
            that.query.params.tenantIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.tenantId = nodesInfo[0].data.id
        this.initOrganTree(function () {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath)
      } else {
        this.initOrganTree(function () {
          that.setOrganQueryType()
        })
      }
    },
    chooseTenantForm(node) {
      let nodesInfo = this.$refs['tenantTreeForm'].getCheckedNodes()
      node.tenantId = null
      let that = this
      if (nodesInfo.length) {
        node.tenantId = nodesInfo[0].data.id
        this.initOrganFormTree(function () {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath, this.organFormOptions)
      } else {
        this.initOrganFormTree(function () {
          that.setOrganQueryType()
        }, null, this.organFormOptions)
      }
    },
    organQueryModelChange(organQueryModel) {
      if (organQueryModel == 'selected') {
        this.props.multiple = true
      } else if (organQueryModel == 'selfAndSub') {
        this.props.multiple = false
      }
    },
    chooseOrganForm(node) {
      let nodesInfo = this.$refs['organTreeForm'].getCheckedNodes()
      if (nodesInfo[0]) {
        this.node.organPath = nodesInfo[0].data.organPath
        this.node.organId = nodesInfo[0].data.id
        this.node.tenantId = nodesInfo[0].data.tenantId
      }
    },
    getGoodsType(form) {
      let nodesInfo = this.$refs['goodsTypeTreeForm'].getCheckedNodes()
      form.goodsTypeId = nodesInfo[0].data.id
      form.goodsTypePath = nodesInfo[0].data.typePath
    },
    handleFiltTenant() {
      let data = getOrganList()
      this.tenantOptions = data.filter(function (item, index) {
        return item.type == 0 || item.type == 1;
      })
    },
    expandChange(row, expandedRows) {
      this.listLoading = true

      this.query.goodsTypePropsValue.goodsPropsId = row.id
      this.query.goodsTypePropsValue.organId = row.organId || getOrganId()
      this.query.goodsTypePropsValue.tenantId = row.tenantId || getTenantId()
      this.query.goodsTypePropsValue.organPath = row.opganPath || getOrganPath()
      this.query.goodsTypePropsValue.currentPage = this.query.goodsTypePropsValue.currentPage|| 1
      this.query.goodsTypePropsValue.pageSize = this.query.goodsTypePropsValue.pageSize || 10

      getGoodsTypePropsValueList(this.query.goodsTypePropsValue,
        this.query.goodsTypePropsValue.currentPage,
        this.query.goodsTypePropsValue.pageSize
      ).then(response => {
        const pager = response.data
        console.log(pager)
        this.goodsTypePropsValueList = pager.entities
        this.query.goodsTypePropsValue.pageSize = pager.pageSize
        this.query.goodsTypePropsValue.currentPage = pager.currentPage
        this.query.goodsTypePropsValue.total = pager.totalCount

        this.listLoading = false
      })
    },
    handleQueryGoodsTypePropsValueList() {

      debugger
      this.query.goodsTypePropsValue.organId = this.query.goodsTypePropsValue.organId || getOrganId()
      this.query.goodsTypePropsValue.organPath = this.query.goodsTypePropsValue.organPath || getOrganId()
      this.query.goodsTypePropsValue.tenantId = this.query.goodsTypePropsValue.tenantId || getOrganId()
      this.query.goodsTypePropsValue.currentPage = this.query.goodsTypePropsValue.currentPage|| 1,
      this.query.goodsTypePropsValue.pageSize = this.query.goodsTypePropsValue.pageSize || 10

      getGoodsTypePropsValueList({ goodsPropsId: this.query.goodsTypePropsValue.goodsPropsId },
        this.query.goodsTypePropsValue.currentPage,
        this.query.goodsTypePropsValue.pageSize
      ).then(response => {
        const pager = response.data
        this.goodsTypePropsValueList = pager.entities
        this.query.goodsTypePropsValue.pageSize = pager.pageSize
        this.query.goodsTypePropsValue.currentPage = pager.currentPage
        this.query.goodsTypePropsValue.total = pager.totalCount

        this.listLoading = false
      })
    },
    handleQueryPropsValSizeChange(val) {
      this.query.goodsTypePropsValue.pageSize = val
      this.query.goodsTypePropsValue.currentPage = 1
      this.handleQueryGoodsTypePropsValueList()
    },
    handleQueryPropsValCurrentChange(val) {
      this.query.goodsTypePropsValue.currentPage = val
      this.handleQueryGoodsTypePropsValueList()
    },
    handleAssignToOrgan(index, row) {
      this.dialogStatus = 'assignOrgan'
      this.dialogAssignOrganFormVisible = true
      this.goodsTypeAssignees.id = row.id
      this.goodsTypeAssignees.organId = row.organId
      this.goodsTypeAssignees.tenantId = row.tenantId
      this.listLoading = true
      this.query.organParams.organPath = getOrganPath()
      this.query.goodsTypesOrganQuery.goodsTypeId = row.id
      this.query.goodsTypesOrganQuery.tenantId = row.tenantId || getTenantId()
      //this.query.goodsTypesOrganQuery.organId = row.organId || getOrganId()
      this.query.goodsTypesOrganQuery.organPath = getOrganPath()
      let that = this
      this.initAssigneeOrgan(this.query.organParams.organPath, function () {

        queryAlreadyAssignedOrgans(that.query.goodsTypesOrganQuery).then(response => {
          debugger
          let data = response.data
          if (data.successful) {
            const data = response.data
            const selectedNodes = data.data
            that.$nextTick(function () {
              that.handleDefaultSelected(selectedNodes)
            })
          } else {
            that.$message({
              type: 'info',
              message: data.description || getSystemErrorCode(data.status)
            })
          }
          that.listLoading = false
        })
      })
    },
    initAssigneeOrgan(filter, fn) {
      let data = getOrganList()
      if (filter) {
        data = data.filter(function (item, index) {
          return item.organPath.indexOf(filter) >= 0
        })
      }
      this.organTree = handleBuildTreeDataStruct(data, 'id', 'parentId')
      if (fn) {
        fn()
      }
    },
    handleDefaultSelected(selectedNodes) {
      for (let i = 0; i < selectedNodes.length; i++) {
        this.$refs.assigneeTree.setChecked(selectedNodes[i].organId, true, false)
      }
    },
    getCheckedNodes() {
      let nodesInfo = this.$refs['assigneeTree'].getCheckedNodes()
      console.log(nodesInfo)
      let that = this
      that.goodsTypeAssignees.assignees = []
      nodesInfo.forEach(function (organ) {
        that.goodsTypeAssignees.assignees.push({
          organId: organ.id,
          tenantId: organ.tenantId,
          organPath: organ.organPath
        })
      })
      assignee2Organ(this.goodsTypeAssignees).then(response => {
        let data = response.data
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        } else {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
        this.listLoading = false
      })

    },
    handleGoodsTypePropsEdit(index, row) {
      this.addForGoodsTypePropsVisible = false
      this.dialogGoodsTypePropsFormVisible = true
      this.goodsTypeProps.id = row.id
      this.goodsTypeProps.goodsTypeId = row.goodsTypeId
      this.goodsTypeProps.propName = row.propName
      this.goodsTypeProps.organId = row.organId || getOrganId()
      this.goodsTypeProps.tenantId = row.tenantId || getTenantId()
      this.goodsTypeProps.organPath = row.organPath || getOrganPath()
      this.goodsTypeProps.updateBy = row.updateBy || getCurrentLoginUserId()
      this.goodsTypeProps.isMust = row.isMust
      this.goodsTypeProps.isNav = row.isNav
      this.goodsTypeProps.isMutilSelect = row.isMutilSelect
      this.goodsTypeProps.updateDate = row.updateDate
      this.goodsTypeProps.remark = row.remark
    },
    handleGoodsTypePropsValueEdit(index, row) {
      debugger
      this.addForGoodsTypePropsValueVisible = false
      this.dialogGoodsTypePropsValueFormVisible = true
      this.goodsTypePropsValue.id = row.id
      this.goodsTypePropsValue.goodsTypeId = row.goodsTypeId
      this.goodsTypePropsValue.goodsPropsId = row.goodsPropsId
      this.goodsTypePropsValue.goodsPropsValue=row.goodsPropsValue
      this.goodsTypePropsValue.organId = row.organId || getOrganId()
      this.goodsTypePropsValue.tenantId = row.tenantId || getTenantId()
      this.goodsTypePropsValue.organPath = row.organPath || getOrganPath()
      this.goodsTypePropsValue.updateBy = row.updateBy || getCurrentLoginUserId()
      this.goodsTypePropsValue.updateDate = row.updateDate
      this.goodsTypePropsValue.remark = row.remark
    }

  }
}

</script>
